import axios from 'axios';
import Qs from 'qs';

// axios 基本配置
axios.create({
  // baseURL: baseURL,
  timeout: 0,
  withCredentials: true,
  headers: { 'X-Requested-With': 'XMLHttpRequest' },
  maxContentLength: 2000,
  transformRequest: [function(data) {
    return Qs.stringify(data);
  }],
  transformResponse: [
    function(data) {
      try {
        data = JSON.parse(data);
      } catch (e) {
        data = {};
      }
      return data;
    }
  ]
});

axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.headers.put['Content-Type'] = 'application/json';

/**
 * 请求拦截器
 */
axios.interceptors.request.use(config => {
  // let url = baseURL + config.url;
  // config.url = url;

  return Promise.resolve(config);
}, function(error) {
  return Promise.reject(error);
});

/**
 * 响应拦截器
 */
axios.interceptors.response.use(response => {
  return response.data;
}, (error) => {
  if (error.response && (error.response.status === 403 || error.response.status === 401)) {


  } else if (error.response && error.response.data && error.response.data.message) {
    if (!(error.config && error.config.ignoreErrorCodes && error.config.ignoreErrorCodes.indexOf(error.response.data.code) !== -1)) {

    }
  }
  return Promise.reject(error);
});


export const get = (url, params) => {
  return axios.get(url, { params: params });
};

export const post = (url, params) => {
  return axios.post(url, params);
};

export const del = (url, params) => {
  return axios.delete(url, params);
};

export const put = (url,params)=>{
  return axios.put(url,params);
};



